import { nextTick } from "vue";
import * as svg from "@element-plus/icons-vue";

// 获取 element-plus 自带的 svg 图标
const getElementPlusIconfont = () => {
	return new Promise((resolve, reject) => {
		nextTick(() => {
			const icons = svg as any;
			const sheetsIconList = [];
			for (const i in icons) {
				sheetsIconList.push(`ele-${icons[i].name}`);
			}
			if (sheetsIconList.length > 0) resolve(sheetsIconList);
			else reject("未获取到 element-plus 图标，请刷新重试");
		});
	});
};

// 获取 font-awesome 的图标
const getAwesomeIconfont = () => {
	return new Promise((resolve, reject) => {
		nextTick(() => {
			const styles = document.styleSheets;
			let iconSheets = {} as EmptyObjectType;
			let iconList = [];
			let breakFlag = false;
			for (let i = 0; i < styles.length; i++) {
				for (let j = 0; j < styles[i].cssRules.length; j++) {
					if (styles[i].cssRules[j].cssText.indexOf("font-family: FontAwesome") > -1) {
						iconSheets = styles[i];
						breakFlag = true;
						break;
					}
				}
				if (breakFlag) break;
			}
			if (iconSheets.cssRules && iconSheets.cssRules.length > 0) {
				for (let i = 0; i < iconSheets.cssRules.length; i++) {
					if (
						iconSheets.cssRules[i].selectorText &&
						iconSheets.cssRules[i].selectorText.indexOf(".fa-") === 0 &&
						iconSheets.cssRules[i].selectorText.indexOf(",") === -1
					) {
						if (/::before/.test(iconSheets.cssRules[i].selectorText)) {
							iconList.push(
								`${iconSheets.cssRules[i].selectorText
									.substring(1, iconSheets.cssRules[i].selectorText.length)
									.replace(/\:\:before/gi, "")}`
							);
						}
					}
				}
				resolve(iconList.reverse());
			} else reject("未获取到 font-awesome 图标，请刷新重试");
		});
	});
};

// 获取 iconfont 的图标
const getAlicdnIconfont = () => {
	return new Promise((resolve, reject) => {
		nextTick(() => {
			const styles = document.styleSheets;
			let iconSheets = {} as EmptyObjectType;
			let iconList = [];
			let breakFlag = false;
			for (let i = 0; i < styles.length; i++) {
				for (let j = 0; j < styles[i].cssRules.length; j++) {
					if (styles[i].cssRules[j].cssText.indexOf("font-family: iconfont") > -1) {
						iconSheets = styles[i];
						breakFlag = true;
						break;
					}
				}
				if (breakFlag) break;
			}
			if (iconSheets.cssRules && iconSheets.cssRules.length > 0) {
				for (let i = 0; i < iconSheets.cssRules.length; i++) {
					if (
						iconSheets.cssRules[i].selectorText &&
						iconSheets.cssRules[i].selectorText.startsWith(".icon-")
					) {
						iconList.push(
							`${iconSheets.cssRules[i].selectorText
								.substring(1, iconSheets.cssRules[i].selectorText.length)
								.replace(/\:\:before/gi, "")}`
						);
					}
				}
				resolve(iconList);
			} else reject("未获取到 iconfont 图标，请刷新重试");
		});
	});
};

/**
 * 获取字体图标 `document.styleSheets`
 * @method ele 获取 element-plus 自带图标
 * @method ali 获取 font-awesome 的图标
 * @method ali 获取 iconfont 的图标
 */
const initIconfont = {
	// element-plus
	ele: () => {
		return getElementPlusIconfont();
	},
	// font-awesome
	awe: () => {
		return getAwesomeIconfont();
	},
	// iconfont
	ali: () => {
		return getAlicdnIconfont();
	},
};

// 导出方法
export default initIconfont;
